package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.CreditShop;
import cn.wolfcode.trip.base.domain.ProductPlus;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.CreditShopQuery;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.ICreditShopService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("products")
public class CreditShopController {
    @Autowired
    private ServletContext ctx;
    @Autowired
    private ICreditShopService creditShopService;
    @GetMapping
    public Object getProducts(CreditShopQuery qo){
        return creditShopService.selectForList(qo);
    }
    @GetMapping("/{productId}")
    public Object getProductById(@PathVariable Long productId){
        return creditShopService.get(productId);
    }
    @RequestMapping("/{productId}/changeCount")
    public void changeCount(@PathVariable Long productId, Long count){
        creditShopService.changeCount(productId, count);
    }


    @GetMapping("/types")
    public void productList(HttpServletResponse rep) throws Exception {

        CreditShopQuery query = new CreditShopQuery();
        query.setState(1);
        query.setIsRecommend(0);
        query.setPageSize(0);

        PageInfo types = creditShopService.getProductTypes(query);

        //模板 + 数据 = 输出
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
        Template template = cfg.getTemplate("productContent.ftl", "utf-8");

        List<CreditShop> list = types.getList();
        for (CreditShop creditShop : list) {
            Integer type = creditShop.getType();
            query.setType(type);
            query.setPageSize(3);
            PageInfo contents = creditShopService.selectForList(query);
            creditShop.setSub(contents);
        }
        types.setList(list);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("types", types);
        //输出到浏览器
        template.process(data, rep.getWriter());
    }
    @GetMapping("/more")
    public void moreProductList(CreditShopQuery qo, HttpServletResponse rep) throws Exception {
        String keyword = qo.getKeyword();
        if (keyword != null) {
            keyword = URLDecoder.decode(keyword,"UTF-8");
            qo.setKeyword(keyword);
        }

        qo.setState(1);
        qo.setIsRecommend(0);
        qo.setPageSize(0);

        PageInfo more =  creditShopService.selectForList(qo);

        //模板 + 数据 = 输出
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
        Template template = cfg.getTemplate("productMore.ftl", "utf-8");

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("more", more);
        //输出到浏览器
        template.process(data, rep.getWriter());
    }
}
